package com.bun.dao;

import com.bun.vo.Config;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;

public class ConfigDao {
	public void updateConfig(Config config){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity configEntity = new Entity("Config","bunipmon");
		configEntity.setProperty("monitor", config.getMonitor());
		configEntity.setProperty("sender", config.getSender());
		configEntity.setProperty("receiver", config.getReceiver());
		configEntity.setProperty("smsFrom", config.getSmsFrom());
		configEntity.setProperty("smsTo", config.getSmsTo());
		configEntity.setProperty("refresh", config.getRefresh());
		configEntity.setProperty("device", config.getDevice());
		configEntity.setProperty("availURL", config.getAvailURL());
		configEntity.setProperty("storeURL", config.getStoreURL());
		
		datastore.put(configEntity);
	}
	
	public Config getConfig(){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("Config");
		//Query q = new Query("Code").addSort("code",SortDirection.ASCENDING);
		Entity result = datastore.prepare(q).asSingleEntity();
		Config config = new Config();
		config.setMonitor((String)result.getProperty("monitor"));
		config.setReceiver((String)result.getProperty("receiver"));
		config.setSender((String)result.getProperty("sender"));
		config.setSmsFrom((String)result.getProperty("smsFrom"));
		config.setSmsTo((String)result.getProperty("smsTo"));
		config.setRefresh((String)result.getProperty("refresh"));
		config.setDevice((String)result.getProperty("device"));
		config.setAvailURL((String)result.getProperty("availURL"));
		config.setStoreURL((String)result.getProperty("storeURL"));
		return config;
	}
	
}
